﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Livestock
    {
        public bool CreateOneLivestock(Livestock_Info objLivestock_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Livestock_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"LivestockID", objLivestock_Info.LivestockID));
            command.Parameters.Add(new SqlParameter(@"LivestockTypeID", objLivestock_Info.LivestockTypeID));
            command.Parameters.Add(new SqlParameter(@"LivestockName", objLivestock_Info.LivestockName));
            command.Parameters.Add(new SqlParameter(@"Breed", objLivestock_Info.Breed));
            command.Parameters.Add(new SqlParameter(@"Blood", objLivestock_Info.Blood));
            command.Parameters.Add(new SqlParameter(@"DOB", objLivestock_Info.DOB));
            command.Parameters.Add(new SqlParameter(@"Weight", objLivestock_Info.Weight));
            command.Parameters.Add(new SqlParameter(@"Description", objLivestock_Info.Description));
            command.Parameters.Add(new SqlParameter(@"MAFFid", objLivestock_Info.MAFFid));
            command.Parameters.Add(new SqlParameter(@"isAlive", objLivestock_Info.isAlive));
            command.Parameters.Add(new SqlParameter(@"FarmerID", objLivestock_Info.FarmerID));
            command.Parameters.Add(new SqlParameter(@"HomeFieldID", objLivestock_Info.HomeFieldID));
            command.Parameters.Add(new SqlParameter(@"HerdID", objLivestock_Info.HerdID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneLivestock(Livestock_Info objLivestock_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Livestock_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@LivestockID", objLivestock_Info.LivestockID));
            command.Parameters.Add(new SqlParameter("@LivestockTypeID", objLivestock_Info.LivestockTypeID));
            command.Parameters.Add(new SqlParameter("@LivestockName", objLivestock_Info.LivestockName));
            command.Parameters.Add(new SqlParameter("@Breed", objLivestock_Info.Breed));
            command.Parameters.Add(new SqlParameter("@Blood", objLivestock_Info.Blood));
            command.Parameters.Add(new SqlParameter("@DOB", objLivestock_Info.DOB));
            command.Parameters.Add(new SqlParameter("@Weight", objLivestock_Info.Weight));
            command.Parameters.Add(new SqlParameter("@Description", objLivestock_Info.Description));
            command.Parameters.Add(new SqlParameter("@MAFFid", objLivestock_Info.MAFFid));
            command.Parameters.Add(new SqlParameter("@isAlive", objLivestock_Info.isAlive));
            command.Parameters.Add(new SqlParameter("@FarmerID", objLivestock_Info.FarmerID));
            command.Parameters.Add(new SqlParameter("@HomeFieldID", objLivestock_Info.HomeFieldID));
            command.Parameters.Add(new SqlParameter("@HerdID", objLivestock_Info.HerdID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneLivestock(int LivestockID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Livestock_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"LivestockID", LivestockID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllLivestock()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Livestock_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public DataTable GetAllLivestockByLivestockType(int livestockTypeID)
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Livestock_SelectAll_ByLivestockType", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"LivestockTypeID", livestockTypeID));
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Livestock_Info GetOneLivestock(int LivestockID)
        {
            Livestock_Info objLivestock_Info = new Livestock_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Livestock_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"LivestockID", LivestockID));
            try
            {
                objConnection.Open();
                SqlDataReader ordLivestock = command.ExecuteReader(); //OjectRecordDatareader
                ordLivestock.Read();
                if (ordLivestock.HasRows)
                {
                    if (ordLivestock["LivestockID"] != DBNull.Value)
                    {
                        objLivestock_Info.LivestockID = Convert.ToInt16(ordLivestock["LivestockID"]);
                    }
                    if (ordLivestock["LivestockTypeID"] != DBNull.Value)
                    {
                        objLivestock_Info.LivestockTypeID = Convert.ToInt16(ordLivestock["LivestockTypeID"]);
                    }
                    if (ordLivestock["LivestockName"] != DBNull.Value)
                    {
                        objLivestock_Info.LivestockName = Convert.ToString(ordLivestock["LivestockName"]);
                    }
                    if (ordLivestock["Breed"] != DBNull.Value)
                    {
                        objLivestock_Info.Breed = Convert.ToString(ordLivestock["Breed"]);
                    }
                    if (ordLivestock["Blood"] != DBNull.Value)
                    {
                        objLivestock_Info.Blood = Convert.ToString(ordLivestock["Blood"]);
                    }
                    if (ordLivestock["DOB"] != DBNull.Value)
                    {
                        objLivestock_Info.DOB = Convert.ToDateTime(ordLivestock["DOB"]);
                    }
                    if (ordLivestock["Weight"] != DBNull.Value)
                    {
                        objLivestock_Info.Weight = Convert.ToInt16(ordLivestock["Weight"]);
                    }
                    if (ordLivestock["Description"] != DBNull.Value)
                    {
                        objLivestock_Info.Description = Convert.ToString(ordLivestock["Description"]);
                    }
                    if (ordLivestock["MAFFid"] != DBNull.Value)
                    {
                        objLivestock_Info.MAFFid = Convert.ToString(ordLivestock["MAFFid"]);
                    }
                    if (ordLivestock["isAlive"] != DBNull.Value)
                    {
                        objLivestock_Info.isAlive = Convert.ToBoolean(ordLivestock["isAlive"]);
                    }
                    if (ordLivestock["FarmerID"] != DBNull.Value)
                    {
                        objLivestock_Info.FarmerID = Convert.ToInt16(ordLivestock["FarmerID"]);
                    }
                    if (ordLivestock["HomeFieldID"] != DBNull.Value)
                    {
                        objLivestock_Info.HomeFieldID = Convert.ToInt16(ordLivestock["HomeFieldID"]);
                    }
                    if (ordLivestock["HerdID"] != DBNull.Value)
                    {
                        objLivestock_Info.HerdID = Convert.ToInt16(ordLivestock["HerdID"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objLivestock_Info;
        }
    }
}
